package cn.itcast.goods.controller.admin;

import cn.itcast.anno.PreAuthorize;
import cn.itcast.goods.dto.SetmealDTO;
import cn.itcast.goods.dto.SetmealPageQueryDTO;
import cn.itcast.goods.service.SetmealService;
import cn.itcast.goods.vo.SetmealVO;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @param SetmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("setmeal:pageQuery")
    @ApiOperation("分页查询")
    public Result<PageBean> pageQuery(SetmealPageQueryDTO SetmealPageQueryDTO) {
        log.info("分页查询：{}", SetmealPageQueryDTO);
        PageBean pageBean = setmealService.pageQuery(SetmealPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @PreAuthorize("setmeal:save")
    @ApiOperation("新增套餐")
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("setmeal:getById")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐：{}", id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @PreAuthorize("setmeal:deleteByIds")
    @ApiOperation("批量删除套餐")
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除套餐：{}", ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @PreAuthorize("setmeal:update")
    @ApiOperation("修改套餐")
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")
    @PreAuthorize("setmeal:startOrStop")
    @ApiOperation("套餐起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("套餐起售停售：{},{}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success();
    }

}